package com.yunmang.utils;


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lin on 2016/7/29.
 */
public class FormatUtils {

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
    private static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    private static DecimalFormat df = new DecimalFormat("0.00");

    public static String format(long time, int type) {
        String str = null;
        switch (type) {
            case 0:
                str = format.format(new Date(time));
                break;
            case 1:
                str = format1.format(new Date(time));
                break;
        }
        return str;
    }

    public static String format(double price) {
        return df.format(price);
    }

    public static String decimal(double price) {
        int sum = (int) (price * 100);
        sum = sum % 100;
        return sum > 9 ? "." + sum : ".0" + sum;
    }


    public static String phone(String phone){
        return phone.substring(0, 3) + "****" + phone.substring(7);
    }

    /**
     * 根据要求去生成指定格式的字符串.后面加上三位
     * @param code
     * @return
     */
    public static String generate(String code){
        return String.format("%3s", code).replace(' ', '0');
    }
}
